package com.yuyakaido.android.cardstackview.sample;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * User: newSalton@outlook.com
 * Date: 2019/11/30 15:11
 * ModifyTime: 15:11
 * Description:
 */
public class TestRecyclerView extends RecyclerView {
    private float mLastXIntercept = 0f;
    private float mLastYIntercept = 0f;

    public TestRecyclerView(@NonNull Context context) {
        super(context);
    }

    public TestRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TestRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // setOnTouchListener(new OnTouchListener() {
        //     @Override
        //     public boolean onTouch(View v, MotionEvent event) {
        //         switch (event.getAction()) {
        //             case MotionEvent.ACTION_DOWN:
        //             case MotionEvent.ACTION_MOVE:
        //                 break;
        //             case MotionEvent.ACTION_UP:
        //                 v.getParent().requestDisallowInterceptTouchEvent(false);
        //                 break;
        //         }
        //         return false;
        //     }
        // });
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        float x = ev.getX();
        float y = ev.getY();
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_MOVE: {
                //横坐标位移增量
                float deltaX = x - mLastXIntercept;
                //纵坐标位移增量
                float deltaY = y - mLastYIntercept;
                if (Math.abs(deltaX) < Math.abs(deltaY)) {
                    Log.e("newsalton", "TestRecyclerView [onInterceptTouchEvent] 垂直");
                } else {
                    Log.e("newsalton", "TestRecyclerView [onInterceptTouchEvent] 水平");
                }
                break;
            }
        }
        mLastXIntercept = x;
        mLastYIntercept = y;

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        TouchEventUtil.logActionMsg(getClass(), "onTouchEvent", e);
        return super.onTouchEvent(e);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        TouchEventUtil.logActionMsg(getClass(), "dispatchTouchEvent", ev);
        return super.dispatchTouchEvent(ev);
    }

    // // @Override
    // // public boolean onInterceptTouchEvent(MotionEvent ev) {
    // //     boolean intercepted = false;
    // //     float x = ev.getX();
    // //     float y = ev.getY();
    // //     //横坐标位移增量
    // //     float deltaX = x - mLastXIntercept;
    // //     //纵坐标位移增量
    // //     float deltaY = y - mLastYIntercept;
    // //     if (Math.abs(deltaX) < Math.abs(deltaY)) {
    // //         intercepted = true;
    // //     } else {
    // //         intercepted = false;
    // //     }
    // //     Log.e("newsalton", "TestRecyclerView [onInterceptTouchEvent] " + intercepted);
    // //     super.onInterceptTouchEvent(ev);
    // //     return intercepted ;
    // // }

    // @Override
    // public boolean onInterceptTouchEvent(MotionEvent ev) {
    //     boolean intercepted = false;
    //     float x = ev.getX();
    //     float y = ev.getY();
    //     int action = ev.getAction() & MotionEvent.ACTION_MASK;
    //     switch (action) {
    //         case MotionEvent.ACTION_DOWN: {
    //             intercepted = false;
    //             //初始化mActivePointerId
    //             super.onInterceptTouchEvent(ev);
    //             break;
    //         }
    //         case MotionEvent.ACTION_MOVE: {
    //             //横坐标位移增量
    //             float deltaX = x - mLastXIntercept;
    //             //纵坐标位移增量
    //             float deltaY = y - mLastYIntercept;
    //             if (Math.abs(deltaX) < Math.abs(deltaY)) {
    //                 intercepted = true;
    //             } else {
    //                 intercepted = false;
    //             }
    //             break;
    //         }
    //         case MotionEvent.ACTION_UP: {
    //             intercepted = false;
    //             break;
    //         }
    //     }
    //     mLastXIntercept = x;
    //     mLastYIntercept = y;
    //     Log.e("newsalton", "TestRecyclerView [onInterceptTouchEvent] " + true);
    //     super.onInterceptTouchEvent(ev);
    //     return true;
    // }

}
